import { Module } from '@nestjs/common';
import { WinstonModule } from 'nest-winston';
import { ConfigModule, ConfigService } from '@nestjs/config';
import * as winston from 'winston';
import * as DailyRotateFile from 'winston-daily-rotate-file';
import * as path from 'path';
import { LoggerService } from './logger.service';

@Module({
  imports: [
    WinstonModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        // 日志级别定义
        const levels = {
          error: 0,
          warn: 1,
          info: 2,
          http: 3,
          debug: 4,
        };

        // 日志级别颜色定义
        const colors = {
          error: 'red',
          warn: 'yellow',
          info: 'green',
          http: 'magenta',
          debug: 'blue',
        };

        // 添加颜色
        winston.addColors(colors);

        // 日志格式定义
        const format = winston.format.combine(
          winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
          winston.format.printf(
            (info) => `${info.timestamp} ${info.level}: ${info.message}`,
          ),
        );

        // 控制台日志格式
        const consoleFormat = winston.format.combine(
          winston.format.colorize({ all: true }),
          winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
          winston.format.printf(
            (info) => `${info.timestamp} ${info.level}: ${info.message}`,
          ),
        );

        // 日志文件的根目录
        const logDir = path.join(process.cwd(), 'logs');

        return {
          levels,
          format,
          transports: [
            // 控制台日志
            new winston.transports.Console({
              format: consoleFormat,
              level: 'debug',
            }),
            // 信息级别日志 - 按日期轮转
            new DailyRotateFile({
              level: 'info',
              dirname: path.join(logDir, 'info'),
              filename: 'info-%DATE%.log',
              datePattern: 'YYYY-MM-DD',
              zippedArchive: true,
              maxSize: '20m',
              maxFiles: '14d',
              format,
            }),
            // 错误级别日志 - 按日期轮转
            new DailyRotateFile({
              level: 'error',
              dirname: path.join(logDir, 'error'),
              filename: 'error-%DATE%.log',
              datePattern: 'YYYY-MM-DD',
              zippedArchive: true,
              maxSize: '20m',
              maxFiles: '30d',
              format,
            }),
            // HTTP请求日志 - 按日期轮转
            new DailyRotateFile({
              level: 'http',
              dirname: path.join(logDir, 'http'),
              filename: 'http-%DATE%.log',
              datePattern: 'YYYY-MM-DD',
              zippedArchive: true,
              maxSize: '20m',
              maxFiles: '14d',
              format,
            }),
          ],
          exitOnError: false,
        };
      },
    }),
  ],
  providers: [LoggerService],
  exports: [WinstonModule, LoggerService],
})
export class LoggerModule {} 